#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

#define high 30
#define width 60

int a[high][width];
int i,j;

void gotoxy(int x,int y)//类似于清屏函数，光标移动到原点位置进行重画
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void startup() {
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			a[i][j]=rand() % 2;
		}
	}
	
}

void show() {
	gotoxy(0,0);
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if (a[i][j]==1)
				printf("*");
			else
				printf(" ");
		}
		printf("\n");
	}

}

void updateWithoutInput() {
	int b[high][width];
	int neibornumber;
	
	for (i=1;i<high-1;i++)
		for (j=1;j<width-1;j++)
			b[i][j]=a[i][j];
	for (i=1;i<high-1;i++)
	{
		for (j=1;j<width-1;j++)
		{
			neibornumber=b[i-1][j-1]+b[i-1][j]+b[i-1][j+1]+b[i][j-1]+b[i][j+1]+b[i+1][j-1]+b[i+1][j]+b[i+1][j+1];
			if (neibornumber==3)
			{
				b[i][j]=1;
			}
			else if (neibornumber==2)
			{
				b[i][j]=a[i][j];
			}
			else
			{
				b[i][j]=0;
			}
		}
	}
	for (i=1;i<high-1;i++)
		for (j=1;j<width-1;j++)
			a[i][j]=b[i][j];
	

}

void updateWithInput() {

}

int main() {
	startup();
	while(1) {
		show();
		updateWithInput();
		updateWithoutInput();
	}

return 0;
}